package com.eight.group.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.eight.group.entity.PageResult;
import com.eight.group.entity.QueryPageBean;
import com.eight.group.mapper.UserMapper;
import com.eight.group.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xqx.eight.group.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author：xingquanxiang createTime：2019/11/3 21:40
 * description:
 */
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户名查询用户信息
     *
     * @param username
     * @return User
     */
    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    /**
     * 根据条件分页查询t_user表
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPageByCondition(QueryPageBean queryPageBean) {
        //调用分页助手api 设置当前页，每页大小
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //执行查询，获取查询结果列表
        List<User> checkGroupList = userMapper.findPageByCondition(queryPageBean.getQueryString());
        //返回 分页实体类封装对象
        return new PageResult(new PageInfo<>(checkGroupList).getTotal(), checkGroupList);
    }

    /**
     * 添加用户
     *
     * @param user    用户
     * @param roleIds 角色ID们
     */
    @Override
    @Transactional
    public void add(User user, Integer[] roleIds) {

        userMapper.add(user);

        if (user.getId() != null && roleIds.length > 0) {
            this.setUserAndRole(user.getId(),roleIds);
        }

    }

    /**
     * 根据UserId查询用户的角色信息
     *
     * @param id
     * @return
     */
    @Override
    public User findById(Integer id) {

        return userMapper.findById(id);
    }

    /**
     * 根据用户ID删除用户 (用户角色关系)
     *
     * @param id 用户ID
     */
    @Override
    @Transactional
    public void delete(Integer id) {

        userMapper.deleteUserAndRole(id);
        userMapper.delete(id);

    }

    @Override
    @Transactional
    public void AlterRole(User user, Integer[] roleIds) {

        userMapper.deleteUserAndRoleByGroupId(user.getId());
        if (user.getId() != null && roleIds.length > 0){
            this.setUserAndRole(user.getId(), roleIds);
        }

    }

    @Override
    public List<User> findAll() {
        List<User> list = userMapper.findAll();
        return list;
    }

    /**
     * 往t_user_role添加数据
     *
     * @param userId
     * @param roleIds
     */
    private void setUserAndRole(int userId, Integer[] roleIds) {
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("userId", userId);
        map.put("roleIds", roleIds);
        userMapper.setUserAndRole(map);
    }


}
